package libraryRun;

import anal.ShowTable;
import deleteBook.DeleteBook;
import deleteUser.DeleteUser;
import insertBook.InsertBook;
import lendBook.LendBook;
import returnBook.ReturnBook;
import searchBook.SearchBook;
import searchLend.SearchLend;
import searchUser.SearchUser;
import updateBook.UpdateBook;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MainFrame extends JFrame{
    private static MainFrame myJFrame;
    private boolean isLogged=false;
    private String managerPassword="admin";
    private String userName;
    private String userId;
    public boolean getLogged() {
        return isLogged;
    }

    public void setLogged(boolean logged) {
        isLogged = logged;
    }

    public String getManagerPassword() {
        return managerPassword;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public MainFrame(){
        super("信息管理系统");
        myJFrame=this;
    }

    public static void main(String[] args) {
        new MainFrame();
        myJFrame.setLayout(null);
        Container container=myJFrame.getContentPane();
        myJFrame.setSize(1200,650);

        //面板
        JPanel p1=new JPanel();
        JPanel p2=new JPanel();
        JPanel p3=new JPanel();
        JPanel p4=new JPanel();
        JPanel p5=new JPanel();
        JPanel p6=new JPanel(new FlowLayout(FlowLayout.CENTER,10,8));
        JPanel p7=new JPanel(new FlowLayout(FlowLayout.CENTER,8,5));
        JPanel p8=new JPanel(new FlowLayout(FlowLayout.CENTER,10,15));
        JPanel p9=new JPanel(null);

        //颜色
        Color color1=new Color(50, 176,255);
        Color color2=new Color(87, 169, 140);
        Color color3=new Color(106, 199, 149);
        Color color4=new Color(66, 192, 163);

        //字体
        Font font=new Font("宋体",Font.BOLD,15);
        Font font1=new Font("宋体",Font.BOLD,50);
        Font font2=new Font("宋体",Font.BOLD,35);
        Font font3=new Font("宋体",Font.BOLD,30);

        //标签
        JLabel label1=new JLabel("图 书 管 理 系 统 ");
        JLabel label2=new JLabel("图 书 管 理");
        JLabel label3=new JLabel("图 书 借 阅");
        JLabel label4=new JLabel("用 户 管 理");
        JLabel label5=new JLabel();

        //按钮
        JButton button1=new JButton("登 录");
        JButton button2=new JButton("注 册");
        JButton button3=new JButton("图 书 查 询");
        JButton button4=new JButton("图 书 添 加");
        JButton button5=new JButton("图 书 修 改");
        JButton button6=new JButton("图 书 删 除");
        JButton button7=new JButton("图 书 借 阅");
        JButton button8=new JButton("图 书 归 还");
        JButton button9=new JButton("借 阅 查 询");
        JButton button10=new JButton("查 询 用 户");
        JButton button11=new JButton("删 除 用 户");
        JButton button12=new JButton("统 计 分 析");


        //文本框
        JTextField field1=new JTextField("未 登 录！",16);
        field1.setEditable(false);
        field1.setHorizontalAlignment(JTextField.CENTER);

        //设置字体
        field1.setFont(font);
        label1.setFont(font1);
        label2.setFont(font2);
        label3.setFont(font2);
        label4.setFont(font2);
        button3.setFont(font3);
        button4.setFont(font3);
        button5.setFont(font3);
        button6.setFont(font3);
        button7.setFont(font3);
        button8.setFont(font3);
        button9.setFont(font3);
        button10.setFont(font3);
        button11.setFont(font3);
        button12.setFont(font3);

        //设置颜色
        p1.setBackground(color1);
        p2.setBackground(color1);
        p3.setBackground(color2);
        p4.setBackground(color3);
        p5.setBackground(color4);

        //设置位置和大小
        p1.setBounds(0,0,1200,60);
        field1.setPreferredSize(new Dimension(1200,60));

        p2.setBounds(980,0,200,60);
        field1.setPreferredSize(new Dimension(200,25));
        button1.setPreferredSize(new Dimension(80,20));
        button2.setPreferredSize(new Dimension(80,20));

        p3.setBounds(0,60,200,220);
        p6.setBounds(200,60,230,220);

        p4.setBounds(0,280,200,200);
        p7.setBounds(200,280,230,200);

        p5.setBounds(0,480,200,150);
        p8.setBounds(200,480,230,150);

        label5.setBounds(0,0,770,575);
        p9.setBounds(430,60,770,575);

        //标签放入面板
        p1.add(label1);
        p3.add(label2);
        p4.add(label3);
        p5.add(label4);

        //登录注册和单行文本放入面板
        p2.add(field1);
        p2.add(button1);
        p2.add(button2);

        //按钮放入面板
        p6.add(button3);
        p6.add(button4);
        p6.add(button5);
        p6.add(button6);

        p7.add(button7);
        p7.add(button8);
        p7.add(button9);
        p7.add(button12);

        p8.add(button10);
        p8.add(button11);

        //放置图片 Icon是接口 Image是抽象类   ImageIcon实现了Icon，可以根据Image绘制Icon，getImage()返回此图标的Image，setImage()设置由此图标的图像
        ImageIcon imageIcon=new ImageIcon("icon/imageIcon1.jpg");
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(770,575,Image.SCALE_AREA_AVERAGING)); //Image中方法，创建并返回此图像的缩放版本
        label5.setIcon(imageIcon);                  // 将图像缩放到的宽度  将图像缩放到的高度 指示用于图像重新取样的算法类型的标志
        p9.add(label5);


        //面板放入容器
        container.add(p2);
        container.add(p1);
        container.add(p3);
        container.add(p4);
        container.add(p5);
        container.add(p6);
        container.add(p7);
        container.add(p8);
        container.add(p9);

        //监听器
        ImageIcon imageIcon1=new ImageIcon("icon/imageIcon6.jpg");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(50,45,Image.SCALE_AREA_AVERAGING));
        button2.addActionListener(new ActionListener() { //注册
            public void actionPerformed(ActionEvent e) {
                Register register=new Register();
                myJFrame.setVisible(false);
                register.registerMethod(register,myJFrame);
            }
        });

        button1.addActionListener(new ActionListener() { //登录
            public void actionPerformed(ActionEvent e) {
                Logging logging=new Logging();
                myJFrame.setVisible(false);
                logging.loggingMethod(logging,myJFrame,field1);
            }
        });

        button3.addActionListener(new ActionListener() { //图书查询
            public void actionPerformed(ActionEvent e) {
                if(!myJFrame.getLogged()){
                    JOptionPane.showMessageDialog(myJFrame,"请先登录！","登录提示",1,imageIcon1);
                    return;
                }
                SearchBook mysearchJFrame=new SearchBook();
                myJFrame.setVisible(false);
                mysearchJFrame.search(mysearchJFrame,myJFrame);
            }
        });

        button4.addActionListener(new ActionListener() { //图书添加
            public void actionPerformed(ActionEvent e) {
                String password=JOptionPane.showInputDialog(myJFrame,"请输入管理员密码","管理员验证",-1);
                if(password==null)
                    return;
                else{
                    if(!myJFrame.getManagerPassword().equals(password)){
                        JOptionPane.showMessageDialog(myJFrame,"管理员密码错误！","管理员验证",1,imageIcon1);
                        return;
                    }
                }
                myJFrame.setLogged(true);
                field1.setText("ID:Admin");
                InsertBook insertB=new InsertBook();
                myJFrame.setVisible(false);
                insertB.insert(myJFrame);
            }
        });

        button5.addActionListener(new ActionListener() { //图书更新
            public void actionPerformed(ActionEvent e) {
                String password=JOptionPane.showInputDialog(myJFrame,"请输入管理员密码","管理员验证",-1);
                if(password==null)
                    return;
                else{
                    if(!myJFrame.getManagerPassword().equals(password)){
                        JOptionPane.showMessageDialog(myJFrame,"管理员密码错误！","管理员验证",1,imageIcon1);
                        return;
                    }
                }
                myJFrame.setLogged(true);
                field1.setText("ID:Admin");
                UpdateBook updateB=new UpdateBook();
                myJFrame.setVisible(false);
                updateB.updateBook(myJFrame);
            }
        });

        button6.addActionListener(new ActionListener() { //图书删除
            public void actionPerformed(ActionEvent e) {
                String password=JOptionPane.showInputDialog(myJFrame,"请输入管理员密码","管理员验证",-1);
                if(password==null)
                    return;
                else{
                    if(!myJFrame.getManagerPassword().equals(password)){
                        JOptionPane.showMessageDialog(myJFrame,"管理员密码错误！","管理员验证",1,imageIcon1);
                        return;
                    }
                }
                myJFrame.setLogged(true);
                field1.setText("ID:Admin");
                DeleteBook deleteBook=new DeleteBook();
                myJFrame.setVisible(false);
                deleteBook.delete(myJFrame);
            }
        });

        button7.addActionListener(new ActionListener() { //图书借阅
            public void actionPerformed(ActionEvent e) {
                if(!myJFrame.getLogged()){
                    JOptionPane.showMessageDialog(myJFrame,"请先登录！","登录提示",1,imageIcon1);
                    return;
                }
                LendBook lendBook=new LendBook();
                myJFrame.setVisible(false);
                lendBook.lend(myJFrame);
            }
        });

        button8.addActionListener(new ActionListener() { //图书归还
            public void actionPerformed(ActionEvent e) {
                if(!myJFrame.getLogged()){
                    JOptionPane.showMessageDialog(myJFrame,"请先登录！","登录提示",1,imageIcon1);
                    return;
                }
                ReturnBook book=new ReturnBook();
                myJFrame.setVisible(false);
                book.returnBook(myJFrame);
            }
        });

        button9.addActionListener(new ActionListener() { //借阅查询
            public void actionPerformed(ActionEvent e) {
                if(!myJFrame.getLogged()){
                    JOptionPane.showMessageDialog(myJFrame,"请先登录！","登录提示",1,imageIcon1);
                    return;
                }
                SearchLend lend=new SearchLend();
                myJFrame.setVisible(false);
                lend.searchLend(myJFrame);
            }
        });

        button10.addActionListener(new ActionListener() { //用户查询
            public void actionPerformed(ActionEvent e) {
                String password=JOptionPane.showInputDialog(myJFrame,"请输入管理员密码","管理员验证",-1);
                if(password==null)
                    return;
                else{
                    if(!myJFrame.getManagerPassword().equals(password)){
                        JOptionPane.showMessageDialog(myJFrame,"管理员密码错误！","管理员验证",1,imageIcon1);
                        return;
                    }
                }
                myJFrame.setLogged(true);
                field1.setText("ID:Admin");
                myJFrame.setVisible(false);
                SearchUser searchUser=new SearchUser();
                searchUser.search(myJFrame);
            }
        });

        button11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String password=JOptionPane.showInputDialog(myJFrame,"请输入管理员密码","管理员验证",-1);
                if(password==null)
                    return;
                else{
                    if(!myJFrame.getManagerPassword().equals(password)){
                        JOptionPane.showMessageDialog(myJFrame,"管理员密码错误！","管理员验证",1,imageIcon1);
                        return;
                    }
                }
                myJFrame.setLogged(true);
                field1.setText("ID:Admin");
                myJFrame.setVisible(false);
                DeleteUser mydeleteUser=new DeleteUser();
                mydeleteUser.deleteUser(myJFrame);
            }
        });

        button12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String password=JOptionPane.showInputDialog(myJFrame,"请输入管理员密码","管理员验证",-1);
                if(password==null)
                    return;
                else{
                    if(!myJFrame.getManagerPassword().equals(password)){
                        JOptionPane.showMessageDialog(myJFrame,"管理员密码错误！","管理员验证",1,imageIcon1);
                        return;
                    }
                }
                myJFrame.setLogged(true);
                field1.setText("ID:Admin");
                myJFrame.setVisible(false);
                ShowTable showTable=new ShowTable();
                showTable.showTable(myJFrame);
            }
        });
        myJFrame.setIconImage((new ImageIcon("icon/imageIcon5.jpg")).getImage()); //改变窗体左上角图标
        myJFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        myJFrame.setLocationRelativeTo(null);
        myJFrame.setResizable(false); //设置是否可由用户调整窗口大小
        myJFrame.setVisible(true);
    }

}
